/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene;

import com.bytedance.scene.navigation.NavigationScene;
import com.bytedance.scene.utlity.Utility;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.ability.fraction.FractionScheduler;
import ohos.agp.components.Component;

/**
 * Created by JiangQi on 9/11/18.
 */
final class LifeCycleFragmentSceneDelegate implements SceneDelegate {
    private final FractionAbility mAbility;
    private final NavigationScene mNavigationScene;
    private final LifeCycleFragment mLifeCycleFragment;
    private final ScopeHolderFragment mScopeHolderFragment;
    private final Boolean mImmediate;

    private boolean mAbandoned = false;

    LifeCycleFragmentSceneDelegate(FractionAbility ability, NavigationScene navigationScene,
                                   LifeCycleFragment lifeCycleFragment,
                                   ScopeHolderFragment scopeHolderFragment,
                                   boolean immediate) {
        this.mAbility = ability;
        this.mNavigationScene = navigationScene;
        this.mLifeCycleFragment = lifeCycleFragment;
        this.mScopeHolderFragment = scopeHolderFragment;
        this.mImmediate = immediate;
    }

    @Override
    public boolean onBackPressed() {
        return !this.mAbandoned && this.mNavigationScene.onBackPressed();
    }

    @Override
    public NavigationScene getNavigationScene() {
        if (this.mAbandoned) {
            return null;
        }
        return this.mNavigationScene;
    }

    @Override
    public final void setNavigationSceneAvailableCallback(NavigationSceneAvailableCallback callback) {
        callback.onNavigationSceneAvailable(this.mNavigationScene);
    }

    @Override
    public void abandon() {
        if (this.mAbandoned) {
            return;
        }
        this.mAbandoned = true;
        final Component view = this.mNavigationScene.getView();
        FractionManager fractionManager = mAbility.getFractionManager();
        FractionScheduler fractionScheduler = fractionManager.startFractionScheduler().remove(this.mLifeCycleFragment).remove(this.mScopeHolderFragment);
        if (this.mImmediate) {
            this.mLifeCycleFragment.setLifecycleFragmentDetachCallback(() -> {
                NavigationSceneUtility.removeTag(mAbility, "");
                if (view != null) {
                    Utility.removeFromParentView(view);
                }
            });
            Utility.commitFragment(fractionManager, fractionScheduler, true);
        } else {
            Utility.commitFragment(fractionManager, fractionScheduler, false);
            NavigationSceneUtility.removeTag(mAbility,"");
            if (view != null) {
                Utility.removeFromParentView(view);
            }
        }
    }
}
